<?php
/**------------------------------------------------------------------------
 * @Author:      lixw
 * @DateTime:    2017-01-11 14:39:15
 * @Description: Description
 * Copyright (c) 2016 All rights reserved.
 *------------------------------------------------------------------------*/
namespace app\wap\controller;

class UserCenter extends Base{

	public $pagesize = 10;

    public function index(){
    	// 跳转至问答首页
		$this->is_foot_bartab  = true;
		$this->is_header       = false;
		$this->is_header_nav   = true;

		$this->add_step("个人中心");
		if($this->_user_id){
			$info = model('User')->user_info($this->_user_id);

			$where['user_id'] = $this->_user_id;
			$ask_count    = model("WapAsk")->getCount($this->_user_id);
			$answer_count = model("WapAskAnswer")->getCount($this->_user_id);

		}

		$this->assign('ask_count', $ask_count);
		$this->assign('answer_count', $answer_count);
		$this->assign('info', $info);
		return $this->view();
    }

    // 我的列表
    public function my_collection(){
    	$this->user_id(true);
    	// $this->is_foot_bartab  = false;
		$this->is_header       = false;
		$this->is_header_nav   = true;

		$title = $this->_get('title');
		$title = urldecode($title);
		$this->add_step($title);

		$model = $this->_get('model');
		$type  = $this->_get('type');
		$type  = $type ? $type : 'question';

		$page = $this->_get('page');
		$page = $page ? $page : 1;
		$query['page'] = $page;

		$list  = model($model)->getList($this->_user_id, $page, $type);

		$except  = $list->total() / $this->pagesize;
		$total_page = ceil($except);

		if($page >= $total_page){
			$is_hasmore = '';
		}else{
			$is_hasmore = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?page=' . ($page + 1);
		}
		

		// 分页
		$this->ajax_next_page($list, $page, $type);
		$this->assign('list', $list);
		$this->assign('type', $type);
		$this->assign('ajax_next_page', $is_hasmore);
    	// dump($list);exit;
    	return $this->view();
    }

    //退出登录
	public function logout(){
		model('User')->clear_login_session();
		$this->redirect(url('Index/index'));
	}

	// 设置
	public function setting(){
		$this->user_id(true);
		// $this->is_foot_bartab  = false;
		$this->is_header       = false;
		$this->is_header_nav   = true;

		return $this->view();
	}

	// 修改基本信息
	public function changeUserBaseInfo(){
		$this->user_id(true);
		if($this->isPost()){
			$data['realname']    = $this->_post('realname', '请填写姓名！');
			$data['sex']         = $this->_post("sex", '请选择性别！');
			$data['province_id'] = $this->_post('province_id', '请选择省份！');
			$data['city_id']     = $this->_post('city_id', '请选择城市！');

			$where['user_id'] = $this->_user_id;
			model('User')->update($data, $where, true);
			// 更新登录信息
			model('User')->get_user_info(true);
			$this->success('修改成功！');
		}
	}

	// 修改旧密码
	public function change_password(){
		$this->user_id(true);
		if($this->isPost()){
			$old_password = $this->user['is_set_password'] ? $this->_post('old_password', "旧密码不能为空！") : '';
			$new_password = $this->_post('new_password', "密码不能为空！");
			$repassword   = $this->_post('repassword', "确认密码不能为空！");

			// $old_password = md5($old_password);
			// $new_password = md5($new_password);
			// 检查两次密码一致性
			if($repassword != $new_password){
				$this->error('两次密码不一致！');
			}

			// 登陆密码对不对
			if(!model("User")->check_password($this->_user_id, $old_password) && $this->user['is_set_password']){
				$this->error("旧密码错误，请检查后再试！");
			}
			// 修改密码
			if(model("User")->change_password($this->_user_id, $old_password, $new_password)){
				$this->success("修改成功", url('Index/index'));
			}else{
				$this->error(model("User")->tips_info);
			}
		}
	}

}
